import 'dart:math';

class StringUtils{
  static bool isNull(String msg){
    if(msg == null){
      return true;
    }
    return msg.isEmpty;
  }

  static bool isEmpty(String msg){
    if(msg == null){
      return true;
    }
    return msg.isEmpty;
  }

  static bool isPhone(String msg){
    if(msg == null){
      return false;
    }
    return msg.length==11 && msg.contains("1");
  }

  ///字符超长需要显示省略时，防止字符串整体省略，插入空格
  static String breakWord(String word){
    if(word == null || word.isEmpty){
      return word;
    }
    String breakWord = '';
    word.runes.forEach((element){
      breakWord += String.fromCharCode(element);
      breakWord +='\u200B';
    });
    return breakWord;
  }

  //格式化秒，返回HH:mm:ss格式
  static String secToTime(int time) {
    String timeStr = "";
    int hour = 0;
    int minute = 0;
    int second = 0;
    if (time <= 0)
      return "00:00:00";
    else {
      minute = time ~/ 60;
      if (minute < 60) {
        second = time % 60;
        timeStr = "00:" + unitFormat(minute) + ":" + unitFormat(second);
      } else {
        hour = minute ~/ 60;
        minute = minute % 60;
        second = time - hour * 3600 - minute * 60;
        if(hour>24){
          timeStr = "${hour~/24}天"+ unitFormat(hour%24) + ":" + unitFormat(minute) + ":" + unitFormat(second);
        }else{
          timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
        }
      }
    }
    return timeStr;
  }

  static String unitFormat(int i) {
    String retStr = "";
    if (i >= 0 && i < 10)
      retStr = "0$i";
    else
      retStr = i.toString();
    return retStr;
  }


  static int compare(String str1, String str2){
    final minCount = min(str1.length, str2.length);
    for (var i = 0; i < minCount; i++) {
      final l1 = str1.codeUnitAt(i);
      final l2 = str2.codeUnitAt(i);
      if(l1>l2){
        return 1;
      }else if(l1<l2){
        return -1;
      }else{
        continue;
      }
    }
    if(str1.length>str2.length){
      return 1;
    }else if(str1.length<str2.length){
      return -1;
    }else{
      return 0;
    }
  }

  ///密码校验，字符、数字、特殊字符包含两种
  static bool checkPwd(String input) {
    RegExp mobile = new RegExp(
        r'^(?![\d]+$)(?![a-zA-Z]+$)(?![~!@#\\$%^&*()_+|<>,.?/:;[\]{}]+$)[\da-zA-Z~!@#\\$%^&*()_+|<>,.?/:;[\]{}]{6,15}$');
    return mobile.hasMatch(input);
  }
}